package com.lagou.order.controller;

import Com.lagou.common.pojo.Goods;
import com.lagou.order.feign.GoodsFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class GoodsController {

    @Autowired
    private GoodsFeign orderFeign;

    @GetMapping("/query/{id}")
    public Goods queryById(@PathVariable Integer id){
        return orderFeign.queryById(id);
    }

    @GetMapping("/port")
    public String getPort(){
        return orderFeign.getPort();
    }

    /**
     * 模拟服务超时 熔断处理
     * @return
     */
    @HystrixCommand(
            //每个方法维护一个线程池
            threadPoolKey = "getPort2",
            threadPoolProperties = {
                    //并发线程数
                    @HystrixProperty(name = "coreSize",value = "1"),
                    //默认线程队列值为-1，默认不开启
                    @HystrixProperty(name = "maxQueueSize",value = "20")
            },
            //设置超时
            commandProperties = {
                    //设置请求的超时时间，一旦请求超过此时间那么都按照超时处理，默认超时时间为1秒
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
            }
    )
    @GetMapping("/getPort2")
    public String getPort2(){

        return orderFeign.getPort();
    }

    /**
     * 服务降级
     */
    @HystrixCommand(
            //超时设置
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
            },
            //设置回退方法
            fallbackMethod = "getGoodsServerFallBack"
    )
    @GetMapping("/getPort3")
    public String getPort3(){

        return orderFeign.getPort();
    }
    /**
     * 回退  当请求出发熔断后执行，补救措施
     */

    public String getGoodsServerFallBack(){
        return "-1";
    }
}
